package com.blood.core.util.token;

import com.blood.core.po.SysData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyPair;
import java.util.Optional;


/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/12 16:28
 * @description RSA 加密 服务
 */
@Service
public class RsaService {

    //    @Autowired
//    private RedisTemplateClient redisTemplate;
    @Autowired
    private SysData sysData;

    /**
     * 获取公钥
     *
     * @return
     */
    public String keyPair() {
        //生成密钥对，一般生成之后可以放到配置文件中
        KeyPair keyPair = RSA.getKeyPair();
        //公钥
        String publicKey = RSA.getPublicKey(keyPair);
        //私钥
        String privateKey = RSA.getPrivateKey(keyPair);
//        redisTemplate.setHashKey(CommonConstant.RSA_KEY, publicKey, privateKey);
//        long timeDiff = DateUtils.getTimeDiff(DateUtils.addClearDay(new Date(), +1), new Date());
//        redisTemplate.expire("key", timeDiff / 1000, TimeUnit.SECONDS);
        return publicKey;
    }

    /**
     * 解密数据
     *
     * @param publicKey
     * @param data
     * @return
     */
    public String decryptContent(String publicKey, String data) {
//        String privateKey = String.valueOf(redisTemplate.getHashKey(CommonConstant.RSA_KEY, publicKey));
        String s = Optional.ofNullable(RSA.decryptByPublicKey(data, publicKey)).orElse(data);
        return s;
    }


}
